package com.cyc.controller;

import com.alibaba.fastjson.JSON;
import com.cyc.entity.Category;
import com.cyc.entity.Product;
import com.cyc.service.CategoryService;
import com.cyc.service.CommentService;
import com.cyc.service.OrderDetailService;
import com.cyc.service.ProductService;
import com.cyc.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("category")
public class CategoryController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    CategoryService categoryService;

    @Autowired
    ProductService productService;

    @Autowired
    CommentService commentService;

    @Autowired
    OrderDetailService orderDetailService;

    @RequestMapping("all")
    @ResponseBody
    public String categoryAll(){

        List<Category> list =(List<Category>) redisUtil.get("cateList");
        if (list==null){
            list = categoryService.selectAll();
        }
        redisUtil.set("cateList",list);
        return JSON.toJSONString(list);
    }

    @RequestMapping("manall")
    @ResponseBody
    public String manall(Integer page, Integer limit, HttpSession session){
        Map<String, Object> map=new HashMap<>();
        session.setAttribute("categorylist",categoryService.selectAll());
        PageHelper.startPage(page,limit);
        List<Category> list = categoryService.selectAll();
        PageInfo pageInfo=new PageInfo(list);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",pageInfo.getTotal());
        map.put("data",list);
        return JSON.toJSONString(map);
    }

    @RequestMapping("del")
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public String del(Integer id){
        Map<String, Object> map=new HashMap<>();
        for (Product product : productService.selectByCategoryId(id)) {
            commentService.deleteByProductId(product.getId());
            orderDetailService.deleteByProductId(product.getId());
        }

        productService.deleteByCateId(id);
        int i = categoryService.deleteByPrimaryKey(id);
        if (i>0){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return JSON.toJSONString(map);

    }

    @ResponseBody
    @RequestMapping(value = "add")
    public String add(Category category){
        Map<String, Object> map=new HashMap<>();
        int i = categoryService.insert(category);
        if (i>0){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Category category){
        Map<String, Object> map=new HashMap<>();
        int i = categoryService.updateByPrimaryKey(category);
        if (i>0){
            map.put("msg","修改成功");
        }else{
            map.put("msg","修改失败");
        }
        return JSON.toJSONString(map);
    }

}
